<div class="content-wrapper">

    <section class="content-header">
        <?php echo $this->partial('layout/breadcrumb'); ?>

        <h1 class="pull-right" id="dberp-menu-top">
            <a href="<?php echo $this->url('extend', ['action'=> 'pluginList']);?>" class="btn btn-primary btn-sm"><i class="fa fa-plus"></i> <?php echo $this->translate('安装扩展插件'); ?></a>
            <?php echo $this->HelpUrl('erpExtend'); ?>
        </h1>

    </section>

    <section class="content">
        <?php echo $this->partial('layout/messages'); ?>

        <div class="row">
            <div class="col-xs-12">
                <div class="box shop-box">
                    <div class="box-body table-responsive no-padding">
                        <table class="table erp-table table-hover">
                            <thead>
                            <tr class="shop-bg-thead">
                                <th width="6%"><?php echo $this->translate('封面'); ?></th>
                                <th width="15%"><?php echo $this->translate('插件名称'); ?></th>
                                <th width="44%"><?php echo $this->translate('插件描述'); ?></th>
                                <th width="12%"><?php echo $this->translate('有效期'); ?></th>
                                <th width="8%"><?php echo $this->translate('状态'); ?></th>
                                <th width="15%"><?php echo $this->translate('操作'); ?></th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php if ($this->pluginList) { foreach ($this->pluginList as $pluginValue) { ?>
                                <tr>
                                    <td><img src="<?php echo 'https://loongdom-public.oss-cn-beijing.aliyuncs.com/upload/plugin/erpImage/' . $pluginValue->getPluginCode() . '.png'; ?>" class="img-thumbnail"></td>
                                    <td>
                                        <?php echo $this->escapeHtml($pluginValue->getPluginName()); ?>
                                        <?php if (isset($this->updatePlugin[$pluginValue->getPluginCode()]) && $this->updatePlugin[$pluginValue->getPluginCode()] > $pluginValue->getPluginVersionNum()) { ?>
                                            &nbsp;<small class="label bg-red"><?php echo $this->translate('有更新'); ?></small>
                                        <?php } ?>
                                    </td>
                                    <td>
                                        <?php echo nl2br($this->escapeHtml($pluginValue->getPluginInfo())); ?><br>
                                        <small>
                                            <?php echo $this->translate('版本') . ' ' . $this->escapeHtml($pluginValue->getPluginVersion()); ?>
                                            &nbsp;|&nbsp;
                                            <?php echo $this->translate('开发者') . ' <a href="'.$pluginValue->getPluginAuthorUrl().'" target="_blank">' . $this->escapeHtml($pluginValue->getPluginAuthor()) . '</a>'; ?>
                                            &nbsp;|&nbsp;
                                            <a href="<?php echo $pluginValue->getPluginSupportUrl(); ?>" target="_blank"><?php echo $this->translate('查看使用教程'); ?></a>
                                            <?php if (!empty($pluginValue->getPluginAdminPath())) { ?>
                                                &nbsp;|&nbsp;
                                                <?php echo $this->translate('安装后位置') . '：' . $this->escapeHtml($pluginValue->getPluginAdminPath()); ?>
                                            <?php } ?>
                                        </small>
                                    </td>
                                    <td>
                                        <?php
                                        if ($pluginValue->getPluginExpired() > 0) {
                                            echo date("Y-m-d", $pluginValue->getPluginExpired());
                                            if ($pluginValue->getPluginExpired() < time()) {
                                                echo ' <span class="text-red text-sm">' . $this->translate('已过期') . '</span>';
                                            }
                                            echo "<p><button type='button' onclick=\"updatePluginLicense('".$pluginValue->getPluginCode()."');\" class='btn btn-xs btn-primary'><i class='fa fa-refresh'></i> ".$this->translate('更新有效期')."</button></p>";
                                        } else {
                                            echo $this->translate('永久');
                                        }
                                       ?>
                                    </td>
                                    <td><?php echo $this->extendHelper()->pluginState($pluginValue->getPluginState()); ?></td>
                                    <td>
                                        <?php if ($pluginValue->getPluginState() == 1) { ?>
                                            <a href="javascript:;" onclick="offPlugin('<?php echo $pluginValue->getPluginCode(); ?>');" class="btn btn-sm btn-warning"><i class="fa fa-power-off" aria-hidden="true"></i> <?php echo $this->translate('点击停用'); ?></a>
                                        <?php } else { ?>
                                            <a href="javascript:;" onclick="onPlugin('<?php echo $pluginValue->getPluginCode(); ?>');" class="btn btn-sm btn-success"><i class="fa fa-check" aria-hidden="true"></i> <?php echo $this->translate('点击启用'); ?></a>
                                            <!--a href="javascript:;" class="btn btn-sm btn-danger"><i class="fa fa-times" aria-hidden="true"></i> <?php echo $this->translate('点击卸载'); ?></a-->
                                        <?php } ?>

                                        <?php if (isset($this->updatePlugin[$pluginValue->getPluginCode()]) && $this->updatePlugin[$pluginValue->getPluginCode()] > $pluginValue->getPluginVersionNum()) { ?>
                                            <button type="button" onclick="updatePlugin('<?php echo $pluginValue->getPluginCode(); ?>');" class="btn btn-sm btn-primary"><i class="fa fa-refresh" aria-hidden="true"></i> <?php echo $this->translate('点击更新'); ?></button>
                                        <?php } ?>
                                    </td>
                                </tr>
                            <?php } } else { ?>
                                <tr>
                                    <td colspan="6">
                                        <p class="text-bold text-red"><?php echo $this->translate('还没有安装扩展插件，点击去安装第一个插件吧!'); ?> <a href="<?php echo $this->url('extend', ['action'=> 'pluginList']); ?>" class="btn btn-sm btn-primary"><i class="fa fa-cloud-download" aria-hidden="true"></i> <?php echo $this->translate('点击安装'); ?></a></p>
                                    </td>
                                </tr>
                            <?php } ?>
                            </tbody>
                        </table>
                    </div>

                </div>

            </div>
        </div>

    </section>
</div>
<script type="text/javascript">
    function onPlugin(pluginCode) {
        layer.msg('<?php echo $this->translate('正在启用插件……'); ?>', {
            icon: 16
            ,shade: 0.01
            ,time: 30000
        });
        $.post("<?php echo $this->url('extend', ['action' => 'onPlugin']); ?>", {pluginCode:pluginCode}, function (data) {
            if (data.state == 'false') {
                layer.alert(data.message, {title: '<?php echo $this->translate('错误提示'); ?>', area:['50%', '70%']});
                return false;
            }
            erpMessage(data.message);
            window.location.reload();
        }, 'json');
    }

    function offPlugin(pluginCode) {
        layer.msg('<?php echo $this->translate('正在停用插件……'); ?>', {
            icon: 16
            ,shade: 0.01
            ,time: 30000
        });
        $.post("<?php echo $this->url('extend', ['action' => 'offPlugin']); ?>", {pluginCode:pluginCode}, function (data) {
            if (data.state == 'false') {
                layer.alert(data.message, {title: '<?php echo $this->translate('错误提示'); ?>', area:['50%', '70%']});
                return false;
            }
            erpMessage(data.message);
            window.location.reload();
        }, 'json');
    }

    function updatePlugin(pluginCode) {
        layer.msg('<?php echo $this->translate('正在更新插件……，请不要关闭页面'); ?>', {
            icon: 16
            ,shade: 0.01
            ,time: 300000
        });
        $.post("<?php echo $this->url('extend', ['action' => 'startUpdatePlugin']); ?>", {pluginCode:pluginCode}, function (data) {
            if (data.state == 'false') {
                layer.alert(data.message, {title: '<?php echo $this->translate('错误提示'); ?>', area:['50%', '70%']});
                return false;
            }
            erpMessage(data.message);
            window.location.reload();
        }, 'json');
    }

    function updatePluginLicense(pluginCode) {
        layer.msg('<?php echo $this->translate('正在更新插件有效期……，请不要关闭页面'); ?>', {
            icon: 16
            ,shade: 0.01
            ,time: 300000
        });
        $.post("<?php echo $this->url('extend', ['action' => 'updatePluginLicense']); ?>", {pluginCode:pluginCode}, function (data) {
            if (data.state == 'false') {
                layer.alert(data.message, {title: '<?php echo $this->translate('错误提示'); ?>', area:['50%', '70%']});
                return false;
            }
            erpMessage(data.message);
            window.location.reload();
        }, 'json');
    }
</script>